<?php
if ($argc < 2) die("\n\tUsage: " . $argv[0]. " [PATH_TO_PROJECT]\n\n");
$myDir = dirname(__FILE__) . "/";
require_once($myDir."../autoload.php");
$path = $argv[1];

$dataObjects = "/data_objects/";
$scaffolders = "/scaffolders/";
$config      = "/config/";
$smartyLocal = "/smarty_local/";
$aggregators = "/data_aggregators/";
$controllers = "/controllers/";
$views       = "/views/";

if (!is_dir($path)) die($path . " doesn't exist");

//create dataobjects directory:
if (!is_dir($path.$dataObjects)) mkdir($path.$dataObjects) or die("failed to create a directory " . $path.$dataObjects );

// create scaffolders directory:
if (!is_dir($path.$scaffolders)) mkdir($path.$scaffolders) or die("failed to create a directory " . $path.$scaffolders );

// create config folder:
if (!is_dir($path.$config)) mkdir($path.$config) or die("failed to create a directory " . $path.$config);

// create config folder:
if (!is_dir($path.$config."web/")) mkdir($path.$config."web/") or die("failed to create a directory " . $path.$config."web/");

//create controllers config:
if (!is_file($path.$config."web/controllers_config.php")) copy($myDir."controllers_config.dry", $path.$config."web/controllers_config.php");

if (!is_dir($path.$smartyLocal)) mkdir($path.$smartyLocal) or die("failed to create a directory " . $path.$smartyLocal);

if (!is_dir($path.$smartyLocal."cache/")) 
  mkdir($path.$smartyLocal."cache/") or die("failed to create a directory " . $path.$smartyLocal."cache/");

if (!is_dir($path.$smartyLocal."compile/")) 
  mkdir($path.$smartyLocal."compile/") or die("failed to create a directory " . $path.$smartyLocal."compile/");

if (!is_dir($path.$smartyLocal."tpls/")) 
  mkdir($path.$smartyLocal."tpls/") or die("failed to create a directory " . $path.$smartyLocal."tpls/");

if (!is_dir($path.$aggregators)) mkdir($path.$aggregators) or die("failed to create a directory " . $path.$aggregators);  

if (!is_dir($path.$controllers)) mkdir($path.$controllers) or die("failed to create a directory " . $path.$controllers);

if (!is_dir($path.$views)) mkdir($path.$views) or die("failed to create a directory " . $path.$views);

if (!is_file($path.$smartyLocal."SmartyLocal.php")) copy($myDir."SmartyLocal.dry", $path.$smartyLocal."SmartyLocal.php");  
  
//put gen_autoload into the config directory:
if (!is_file($path.$config. "gen_autoload.php")) copy($myDir."gen_autoload.dry", $path.$config."gen_autoload.php");

//put gen_autoload into the config directory:
if (!is_file($path.$config. "config.php")) {
  $cfg = file_get_contents($myDir."config.dry");
  $cfg = str_replace('__PATH_TO_DRYLAMP__', str_replace("console/","",$myDir), $cfg);
  file_put_contents($path.$config."config.php",$cfg);
}

//put gen_autoload into the config directory:
if (!is_file($path. "/user_autoload.php")) copy($myDir."user_autoload.dry", $path."user_autoload.php");

//put AppController.php into controllers directory:
if (!is_file($path. $controllers."AppController.php")) copy($myDir."AppController.dry", $path.$controllers."AppController.php");

//create public_html
if (!is_dir($path."/public_html/")) mkdir($path."public_html/") or die("Can't create a directory ". $path."public_html/");

//create etc
if (!is_dir($path."/etc/")) mkdir($path."etc/") or die("Can't create a directory ". $path."etc/");

//create cron:
if (!is_dir($path."/cron/")) mkdir($path."cron/") or die("Can't create a directory ". $path."cron/");
if (!is_file($path."/cron/cron_common.php")) copy($myDir. "cron_common.dry", $path."cron/cron_common.php");
if (!is_file($path."/cron/install_all.php")) copy($myDir. "install_all.dry", $path."cron/install_all.php");


//put .htaccess
if (!is_file($path. "public_html/.htaccess")) copy($myDir."htaccess.dry", $path. "public_html/.htaccess");

//put index file:
if (!is_file($path. "public_html/index.php")) copy($myDir."index.dry", $path. "public_html/index.php");
